package com.nana.framework.ui.manager
{
	import com.nana.framework.base.App;
	import com.nana.framework.ui.ITipComponent;
	import com.nana.framework.ui.manager.base.IFollowToolTip;
	import com.nana.framework.ui.manager.base.IToolTip;
	import com.nana.framework.ui.manager.base.ToolTipPool;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	public class ToolTipManager {
		
		public static const DOWN:String = "down";
		public static const UP:String = "up";
		public static const LEFT:String = "left";
		public static const RIGHT:String = "right";
		
		private var _stage:Stage;
		private var _parent:DisplayObjectContainer;
		private var _popTipsParent:DisplayObjectContainer;
		private var _toolTipList:Object;
		protected var _mouseOverToolTipList:Dictionary;
		private var _currentTooltip:IToolTip;
		
		public function ToolTipManager(){
			_toolTipList = {};
			super();
			_stage = App.appStage;
			_stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
			_mouseOverToolTipList = new Dictionary;
		}
		
		public function get parent():DisplayObjectContainer{
			return (_parent || _stage);
		}
		
		public function set parent(parentDo:DisplayObjectContainer):void{
			_parent = parentDo;
		}
		
		public function get popTipsParent():DisplayObjectContainer{
			return _popTipsParent;
		}
		
		public function set popTipsParent(popParent:DisplayObjectContainer):void{
			_popTipsParent = popParent;
		}
		
		public function registerToolTip(toolTipClass:Class, toolTipName:String="simple"):void{
			_toolTipList[toolTipName] = toolTipClass;
		}
		
		private function mouseMoveHandler(event:MouseEvent):void{
			this.mouseMove(_currentTooltip);
		}
		
		public function mouseMove(toolTip:IToolTip):void{
			if ((toolTip != null) && (toolTip is IFollowToolTip)){
				if ((toolTip as DisplayObject).visible == true){
					(toolTip as IFollowToolTip).move(this._stage.mouseX, this._stage.mouseY);
				}
			}
		}
		
		public function mouseOver(display:Object):void{
			var tipComponent:ITipComponent;
			if ((display is ITipComponent)){
				tipComponent = (display as ITipComponent);
				if (!(tipComponent.toolTip == "") || !(tipComponent.toolTipData == null)){
					this.show(tipComponent, tipComponent.toolTip, tipComponent.toolTipDirection, tipComponent.toolTipData, tipComponent.toolTipType);
				}
			}
		}
		
		public function mouseOut(_arg1:Object):void{
			var _local2:ITipComponent;
			if ((_arg1 is ITipComponent)){
				_local2 = (_arg1 as ITipComponent);
				this.hide(_local2);
			}
		}
		
		public function show(toolTipObj:Object, tip:String="", direction:String="down", data:Object=null, toolTipName:String="simple"):void{
			var toolTipClass:Class = _toolTipList[toolTipName];
			var toolTip:IToolTip = ToolTipPool.getToolTip(toolTipClass);
			_currentTooltip = toolTip;
			_mouseOverToolTipList[toolTipObj] = toolTip;
			if ((this.popTipsParent != null) && (UIManager.getPopUpManager().getWindow() != null)){
				this.popTipsParent.addChild((toolTip as DisplayObject));
			} else {
				this.parent.addChild((toolTip as DisplayObject));
			}
			toolTip.show(toolTipObj, tip, direction, data);
			if (toolTip is IFollowToolTip){
				(toolTip as IFollowToolTip).move(_stage.mouseX, _stage.mouseY);
			}
		}
		
		public function hide(toolTipObj:Object):void{
			if (_mouseOverToolTipList[toolTipObj] != null){
				(_mouseOverToolTipList[toolTipObj] as IToolTip).hide();
				delete _mouseOverToolTipList[toolTipObj];
			}
		}
		
		public function get currentTooltip():IToolTip{
			return _currentTooltip;
		}
		
	}
}